package com.qire.manhua.controller;

import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.constant.Navigation;
import com.qire.common.support.base.EventController;
import com.qire.common.utils.OSUtil;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.activity.ComicCommentListActivity;
import com.qire.manhua.databinding.LayoutComicCommentListItemBinding;
import com.qire.manhua.dialog.CommentComplaintDialog;
import com.qire.manhua.dialog.ComicCommentOptionMenu;
import com.qire.manhua.model.viewModel.impl.ComicCommentListViewModel;
import com.qire.manhua.model.viewModel.impl.ComicCommentListViewModel.ComicCommentListItemViewModel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import androidx.annotation.NonNull;

/**
 * 事件
 */
public class ComicCommentListController extends EventController<ComicCommentListActivity> implements AutoVariable<LayoutComicCommentListItemBinding> {

    private ComicCommentListViewModel viewModel;

    public ComicCommentListController(@NonNull ComicCommentListActivity activity) {
        super(activity);
    }

    @Override
    protected void bindView() {
        owner.viewDataBinder().setComicCommentListController(this);
    }

    @Override
    public void bindVariable(LayoutComicCommentListItemBinding viewBind) {
        viewBind.setComicCommentListController(this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(ComicCommentListViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    public void refreshLayoutOnLoadMore(RefreshLayout refreshLayout) {
        viewModel.moreCommentData();
    }
    public void refreshLayoutOnRefresh(RefreshLayout refreshLayout) {
        viewModel.refreshCommentData();
    }

    public void backBtnOnClick(View view){
        owner.onBackPressed();
    }

    public void editCommentBtnOnClick(View view) {
        // 编辑评论
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicCommentPage)
                .withData("comicBookEntity", viewModel.comicBookEntity())
                .navigation();
    }

    //region #评论子项按钮事件 begin
    /**
     * 回复这条评论 列表项目单击事件
     * */
    public void commentItemOnClick(View view) {
        ComicCommentListItemViewModel commentItemViewModel = (ComicCommentListItemViewModel) view.getTag();
        if(commentItemViewModel == null) {
            return;
        }
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicCommentReplyPage)
                .withData("comicComment", commentItemViewModel.comicComment)
                .withData("comicBookEntity", viewModel.comicBookEntity())
                .navigation();
    }

    public boolean commentItemOnLongClick(View view) {
        final ComicCommentListItemViewModel commentItemViewModel = (ComicCommentListItemViewModel) view.getTag();
        if(commentItemViewModel == null) {
            return false;
        }
        ComicCommentOptionMenu menu = new ComicCommentOptionMenu(view.getContext());
        menu.setComplaintListener(v -> {
            v.setTag(commentItemViewModel);
            complaintBtnOnClick(v);
            v.setOnClickListener(null);
        }).setReplyListener(v -> {
            v.setTag(commentItemViewModel);
            writeAdditionalComment(v);
            v.setOnClickListener(null);
        }).setCopyListener(v ->{
            String text = commentItemViewModel.content();
            OSUtil.copyToClipboard(view.getContext(), "Label", text);
            ToastHelper.showToast("复制成功");
            v.setOnClickListener(null);
        }).show();
        return true;
    }

    /**
     * 举报这条评论 列表项目单击事件
     * */
    public void complaintBtnOnClick(View view) {
        ComicCommentListItemViewModel commentItemViewModel = (ComicCommentListItemViewModel) view.getTag();
        if(commentItemViewModel == null) {
            return;
        }
        CommentComplaintDialog dialog = new CommentComplaintDialog(view.getContext());
        dialog.setCommentInfo(commentItemViewModel.userName(), commentItemViewModel.content());
        dialog.setListener((int number, String text) -> commentItemViewModel.complaint(number, text));
        dialog.show();
    }
    /**
     * 给评论点赞 列表项目单击事件
     * */
    public void giveThumbsUpBtnOnClick(View view) {
        ComicCommentListItemViewModel commentItemViewModel = (ComicCommentListItemViewModel) view.getTag();
        if(commentItemViewModel == null) {
            return;
        }
        if(commentItemViewModel.isMyLike()) {
            ToastHelper.showToast("您已经点过赞了");
            return;
        }
        commentItemViewModel.giveAlike();
    }

    public void writeAdditionalComment(View view) {
        ComicCommentListItemViewModel commentItemViewModel = (ComicCommentListItemViewModel) view.getTag();
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicCommentReplyPage)
                .withData("comicComment", commentItemViewModel.comicComment)
                .withData("comicBookEntity", viewModel.comicBookEntity())
                .navigation();
    }

    //endregion #评论子项按钮事件 end

}
